<?php
namespace jy_juanfee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	//计划任务
	public function up_jifen(){
		//昨天
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$lastdaytime = $daytime-24*3600;
		$isadd=$this->get('feelog',array('type'=>1,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'id');
		if($isadd['id']){
			return false;
		}
		if($this->setting['isopen']){
            $num=0;
            $jifen1=0;
            $jifen4=0;
            $members=$this->getall('member',array('dayjifen1 >'=>0,'lastjifen1 >'=>0,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','lastjifen1','dayjifen1'));
            foreach($members as $mv){
                    $fee=$mv['dayjifen1']>$mv['lastjifen1']?$mv['lastjifen1']:$mv['dayjifen1'];
                    if(!$fee || $fee<0){
                        continue;
                    }
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
                        'ordersn'=>'每日获取',
						'money'=>0,
						'jifen'=>$fee,
						'createtime'=>TIMESTAMP,
						'status'=>2,
						'type'=>2,
					);
					$this->insert('commission',$new_commission);
                    model('core/member')->member_credit($mv['mid'],'credit1',$fee,true,'jy_juanfee','每日释放自购积分');
                    $update=array(
                        'lastjifen1 -='=>$fee,
                    );
                    $this->update('member',$update,array('mid'=>$mv['mid'],'uniacid'=>$_SESSION['uniacid']));
                    $num +=1;
                    $jifen1 +=$fee;
            }
            $members=$this->getall('member',array('dayjifen4 >'=>0,'lastjifen4 >'=>0,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','lastjifen4','dayjifen4'));
            foreach($members as $mv){
                    $fee=$mv['dayjifen4']>$mv['lastjifen4']?$mv['lastjifen4']:$mv['dayjifen4'];
                    if(!$fee || $fee<0){
                        continue;
                    }
                    $new_commission=array(
                        'uniacid'=>$_SESSION['uniacid'],
                        'mid'=>$mv['mid'],
                        'ordersn'=>'每日获取',
                        'money'=>0,
                        'jifen'=>$fee,
                        'createtime'=>TIMESTAMP,
                        'status'=>2,
                        'type'=>1,
                    );
                    $this->insert('commission',$new_commission);
                    model('core/member')->member_credit($mv['mid'],'credit4',$fee,true,'jy_juanfee','每日释放自购兑换券');
                    $update=array(
                        'lastjifen4 -='=>$fee,
                    );
                    $this->update('member',$update,array('mid'=>$mv['mid'],'uniacid'=>$_SESSION['uniacid']));
                    $num +=1;
                    $jifen4 +=$fee;
            }
		}
		$log=array(
			'num'=>$num,
			'jifen1'=>$jifen1,
			'jifen4'=>$jifen4,
		);
		$this->add_feelog($log,1);
		return true;
	}
	public function add_feelog($log=array(),$type){
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
            'num'=>$log['num'],
			'jifen1'=>$log['jifen1'],
			'jifen4'=>$log['jifen4'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
}